# coding:utf8
from drf_haystack.serializers import HaystackSerializer

from articles.models import Article, Channel, Comment
from rest_framework import serializers

from articles.search_indexes import ArticleIndex
from users.models import User


class ChannelSerializers(serializers.ModelSerializer):
    '''频道列表'''

    class Meta:

        model = Channel

        fields = ('id','name')


class InnerArticleChannelViewSerializers(serializers.ModelSerializer):


    class Meta:

        model = Article

        fields = ('id','title')


class UserArticleChannelViewSerializers(serializers.ModelSerializer):

     articles = InnerArticleChannelViewSerializers(read_only=True,many=True)

     class Meta:

         model = User

         fields = ('id','username','avatar','articles','fans')



class ArticleChannelViewSerializers(serializers.ModelSerializer):
    '''文章'''
    user = UserArticleChannelViewSerializers(read_only=True)

    collected = serializers.BooleanField(label='收藏',default=False)

    class Meta:

        model = Article

        fields = ('id','title','content','createtime','user','collected_users','collected','image')


class UploadArticleSerializers(serializers.ModelSerializer):
    '''上传文章'''

    class Meta:

        model = Article

        fields = ('user','content','labels','title','channel','image')

        extra_kwargs = {
            "content":{"required":True},
            "title": {"required": True},
            "channel":{"required": True},
            "labels": {"required": True},
            "image": {"required": True},
            "user": {"required": True,"write_only":True}

        }


class UserCollectArticleSerializer(serializers.ModelSerializer):

    class Meta:

        model = User

        fields = '__all__'


class CommentArticleSerializer(serializers.ModelSerializer):

    class Meta:

        model = Comment

        fields = ('id','article','content','parent','user')


class DetailedInnerArticleSerializers(serializers.ModelSerializer):

    class Meta:

        model = Article

        fields = ('id','title')


class DetailedArticleUserSerializers(serializers.ModelSerializer):

    articles = DetailedInnerArticleSerializers(read_only=True,many=True)

    class Meta:

        model = User

        fields = ('id','username','articles','fans','avatar')


class DetailedArticleInnerCommentSerializers(serializers.ModelSerializer):

    user = DetailedArticleUserSerializers(read_only=True)

    subs = serializers.StringRelatedField(read_only=True,many=True)

    class Meta:

        model = Comment

        fields = '__all__'


class DetailedArticleCommentSerializers(serializers.ModelSerializer):

    user = DetailedArticleUserSerializers(read_only=True)

    subs = DetailedArticleInnerCommentSerializers(read_only=True,many=True)

    class Meta:

        model =Comment

        fields = '__all__'


class DetailedArticleViewSerializers(serializers.ModelSerializer):
    '''文章'''
    user = UserArticleChannelViewSerializers(read_only=True)

    collected_users = serializers.StringRelatedField(read_only=True,many=True)

    comments = DetailedArticleCommentSerializers(read_only=True,many=True)

    class Meta:

        model = Article

        fields = ('id','user','comments','title','content','createtime','updatetime','visits',
                  'thumbup','comment_count','channel','labels','collected_users','image')



class ArticleHaystackSerializer(HaystackSerializer):

    class Meta:

        index_classes = [ArticleIndex]

        fields = ('id', 'text', 'createtime', 'content', 'title')